﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Magic;
using System.Diagnostics;
using Logic;
using System.Runtime.InteropServices;
using System.Threading;
using Logic.Mappings;

namespace T4CHelper
{
    public partial class Form1 : Form
    {
        public Process T4CReference { get; set; }
        Player player;
        PlayerParameters parameters;
        T4CMouse mouse;

        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        const uint WM_KEYDOWN = 0x100;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Process.GetProcessesByName("T4C").Count() > 0)
            {
                T4CReference = Process.GetProcesses().Single(c => c.ProcessName == "T4C");
                Pulse.Enabled = true;
                parameters = new PlayerParameters();
                parameters.Handle = T4CReference.Handle;
                parameters.HealMacro = tbHealMacro.Text;
                parameters.HealPercent = (double)tbHeal.Value / 100;
                //parameters.CurrentPots = int.Parse(tbtotalpots.Text);
                parameters.RecallMacro = tbRecallMacro.Text;
                player = new Player(int.Parse(tbtotalpots.Text));
                mouse = new T4CMouse();
                Pulse.Start();
            }
            else
                MessageBox.Show("You must launch T4C first, then start the AutoPopo feature.", "T4C not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void Pulse_Tick(object sender, EventArgs e)
        {
            mouse.Update(parameters);
            player.Update(parameters);
            tbScannedPlayerID.Text = mouse.OverID.ToString();

        }

        private void bStop_Click(object sender, EventArgs e)
        {
            Pulse.Stop();
            Pulse.Enabled = false;
        }

        private void tbHeal_ValueChanged(object sender, EventArgs e)
        {
            lbHealStep.Text = string.Format("You will use pots when you hit {0}% HP", ((TrackBar)sender).Value);
        }

        private void tbtotalpots_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void bUp_Click(object sender, EventArgs e)
        {
            SendKeys.SendWait("{UP}");
        }

        private void bLeft_Click(object sender, EventArgs e)
        {
            BringWindowToTop(Process.GetProcesses().Single(c => c.ProcessName == "T4C").Handle);
        }

        private void bRight_Click(object sender, EventArgs e)
        {
            SendKeys.SendWait("{RIGHT}");

        }

        private void bDown_Click(object sender, EventArgs e)
        {
            SendKeys.SendWait("{DOWN}");

        }

        [DllImport("user32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetActiveWindow(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool BringWindowToTop(IntPtr hWnd);

        private void cbRecall_CheckedChanged(object sender, EventArgs e)
        {
            if (parameters != null)
                parameters.EnableRecall = (sender as CheckBox).Checked;
            else
                MessageBox.Show("You must launch T4C first, then start the AutoRecall feature.", "T4C not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {


        }
    }
}
